// merchant//share/jiuCardCheck/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cid: 0,
    cardType: 0,//4玖玖卡（27059）5福鲤卡（27111）
    clientUid: 0,
    cardItemInfo: {},
    inputMoney: '',
    showPopups: false,
    createUidList: [],//商家列表
    showMore: false,
    currentUid: 0,
    currentName: ''
  },
  showMore: function(){
    this.setData({
      showMore: !this.data.showMore
    })
  },
  changeCreateUid: function(e){
    this.data.currentUid = e.currentTarget.dataset.uid;
    this.getCardItem(e.currentTarget.dataset.uid);
    this.setData({
      currentName: e.currentTarget.dataset.name,
      showMore: !this.data.showMore
    })
  },
  // 获取核销员所属商家
  getMerchant: function(){
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/staff/GetListByCheck?uid=' + app.globalData.userInfo.Uid,
      success: function(res){
        let createUid = '';
        if(res.data.Code == 0){
          if(res.data.Data && res.data.Data.length > 0){
            createUid = res.data.Data[0].Company;
            that.setData({
              createUidList: res.data.Data,
              currentName: res.data.Data[0].Company,
              currentUid: res.data.Data[0].Uid
            })
            that.getCardItem(createUid);
          }else{
            that.getCardItem(createUid);
          }
        }else{
          that.getCardItem(createUid);
        }
      }

    })
  },
  getCardItem: function (createUid='') {
    let that = this;
    if(!createUid){
      createUid = app.globalData.userInfo.Uid;
    }
    wx.request({
      url: app.globalData.host + '/api/Coupon/GetCardUser?cid=' + that.data.cid + '&createUid=' + createUid,
      success: function (res) {
        if (res.data.Code == 0) {
          if (res.data.Data) {
            let cardItemInfo = res.data.Data;
            cardItemInfo.ExpireTime = cardItemInfo.ExpireTime.substr(0, 10);
            that.setData({
              cardItemInfo: cardItemInfo
            })
          }
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },
  inputMoney: function (e) {
    this.setData({
      inputMoney: e.detail.value
    })
  },

  submitCheck: function () {
    let that = this;
    let inputMoney = that.data.inputMoney;
    let cardItemInfo = that.data.cardItemInfo;
    let discount = cardItemInfo.Discount;
    if (!inputMoney) {
      that.checkWrite('请输入消费金额');
      return
    }
    if (inputMoney % 1 != 0) {
      var checkMoney = that.checkNumbers(inputMoney);
      if (!checkMoney) {
        that.checkWrite("请输入正确的金额");
        return;
      }
    }
    let cardpay = parseFloat(inputMoney) * parseFloat(discount) / 100;//从卡里扣除的金额
    let realpay = 0;//支付金额
    if (cardItemInfo.cardType == 4) {//玖玖卡
      if (cardpay > cardItemInfo.Balance) {
        cardpay = cardItemInfo.Balance;
      }
      cardpay = cardpay.toFixed(2);
      realpay = parseFloat(inputMoney) - parseFloat(cardpay);//支付金额
      realpay = realpay.toFixed(2);
    } else {//福鲤卡
      if (cardpay > cardItemInfo.Balance) {
        realpay = (parseFloat(cardpay) - parseFloat(cardItemInfo.Balance)).toFixed(2);//支付金额
        cardpay = cardItemInfo.Balance.toFixed(2);
      }
    }
    that.setData({
      showPopups: true,
      realpay: realpay,
      cardpay: cardpay
    })
  },
  submitCardItem: function () {
    let that = this;
    that.closePopups();
    let money = "-" + that.data.cardpay;
    let payMoney = that.data.realpay;
    wx.showLoading({
      title: '提交中...',
      mask: true
    })
    wx.request({
      url: app.globalData.host + '/api/Coupon/UpdateCardUserBlance',
      data: {
        cid: that.data.cid,
        amount: money, //金额（次数）
        checkUid: app.globalData.userInfo.Uid,
        score: 0,
        payMoney: parseFloat(payMoney), //应付金额
        expireTime: that.data.cardItemInfo.ExpireTime,
        direction: 2,
        createUid: that.data.currentUid
      },
      method: 'POST',
      success: function (res) {
        wx.hideLoading();
        if (res.data.Code == 0) {
          wx.showToast({
            title: '核销成功',
            duration: 2000,
            mask: true
          })
          setTimeout(function () {
            wx.navigateBack({
              delta: 1
            })
          }, 2000);
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },
  // 关闭弹窗
  closePopups: function () {
    this.setData({
      showPopups: false
    })
  },

  // 小数点验证
  checkNumbers: function (value) {
    var returnVal = true;
    var inputZ = value;
    var ArrMen = inputZ.split("."); //截取字符串
    if (ArrMen.length > 2) {
      returnVal = false;
    }
    return returnVal;
  },
  //填写判断
  checkWrite: function (str) {
    wx.showModal({
      title: '提示',
      content: str,
      showCancel: false
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options) {
      this.data.cid = options.cid;
      this.data.clientUid = options.clientuid;
      this.setData({
        cardType: options.cardtype
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    if (app.globalData.userInfo && that.data.cid > 0) {
      that.getMerchant();
    } else {
      setTimeout(function () {
        this.onShow();
      }.bind(that), 1000)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})